using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;



namespace GameFirst
{

    public class Gamerendering : Microsoft.Xna.Framework.DrawableGameComponent
    {
        renderingdata rendering_data;
        SpriteBatch spriteBatch;
        BasicEffect basic_effect;
        Texture2D mirino;
        Texture2D pause;
        Texture2D pauses;
        Texture2D medal;
        SpriteFont font;
        Vector2 string_sizeP;
        Vector2 string_sizeB;
        Game G;
        public Gamerendering(Game game)
            : base(game)
        {
            G = game;
            rendering_data = new renderingdata();
            Game.Services.AddService(typeof(renderingdata), rendering_data);
        }

        public override void Initialize()
        {


            base.Initialize();

            /*non renderizzare i triangoli non visti
            Clockwise 	Eliminare selettivamente le facce posteriori con vertici in senso orario. 
            CounterClockwise 	Eliminare selettivamente le facce posteriori con vertici in senso antiorario. 
            None 	Non eliminare selettivamente le facce posteriori. */
            GraphicsDevice.RasterizerState = new RasterizerState()
            {
                CullMode = CullMode.CullCounterClockwiseFace
            };
        }


        protected override void LoadContent()
        {
            spriteBatch = new SpriteBatch(GraphicsDevice);
            basic_effect = new BasicEffect(GraphicsDevice);
            //PER VEDERE LE BOUNDING SPHERE E BOX
            //DebugShapeRenderer.Initialize(GraphicsDevice);

            //PER IL 3D old
            //20 è la z che viene verso di te, 30 è la x che va verso destra, 50 è la y che va verso l'altro
            //Vector3 poscan= new Vector3(new Vector2(30.0f , 50.0f), 20.0f);

            //Vector3 poscan = new Vector3(new Vector2(-1.0f, -12.0f),-3.0f);
            //Vector3 poscan = new Vector3(new Vector2(1.0f, 1.0f), 1.0f);
            //basic_effect.World = Matrix.CreateWorld(poscan, Vector3.Forward, Vector3.Up);
            //1 prova funziona circa
            //basic_effect.View = Matrix.CreateLookAt(
            //      Vector3.Backward * 350.0f + Vector3.Down * 150.0f,
            //      Vector3.Down * 150.0f,
            //      Vector3.Up);
            //basic_effect.Projection = Matrix.CreatePerspectiveFieldOfView(
            //        1.7f,//MathHelper.ToRadians (45.0f)
            //        GraphicsDevice.Viewport.AspectRatio,
            //        0.1f, 150.0f);
            //2 prova funziona circa ma è meglio della prima

            //   ORTOGONALE!!!!!
            basic_effect.World = Matrix.CreateWorld(new Vector3(new Vector2(0.0f, 0.0f), 0.0f), Vector3.Forward, Vector3.Up);
            basic_effect.View = Matrix.CreateLookAt(
                  new Vector3(GraphicsDevice.Viewport.Width / 2, -GraphicsDevice.Viewport.Height / 2, +100.0f),
                  new Vector3(GraphicsDevice.Viewport.Width / 2, -GraphicsDevice.Viewport.Height / 2, 0.0f),
                  Vector3.Up);
            basic_effect.Projection = Matrix.CreateOrthographicOffCenter(0.0f, (float)GraphicsDevice.Viewport.Width, 0.0f, (float)GraphicsDevice.Viewport.Height, -10, +5000);
           
            //PROVE
            //Matrix world = Matrix.CreateScale(3f) * Matrix.CreateTranslation(new Vector3(100, 100, 0)) ;
            //Matrix view = Matrix.CreateLookAt(new Vector3(0.0f, 0.0f, 1.0f), Vector3.Zero, Vector3.Up);
            //Matrix projection = Matrix.CreateOrthographicOffCenter(
            //    0, (float)GraphicsDevice.Viewport.Width, (float)GraphicsDevice.Viewport.Height, 0, 1.0f, 1000.0f); 
            //basic_effect.World = Matrix.Identity;
            //basic_effect.View = Matrix.CreateLookAt(
            //      Vector3.Backward  ,
            //      Vector3.Zero,
            //      Vector3.Up);
            //basic_effect.Projection = Matrix.CreateOrthographic(GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height, -10000, +10000);


            font = Game.Content.Load<SpriteFont>("fontnuovo");
            string_sizeP = font.MeasureString("Points: "+ AirSimulation.statechange.Points );
            string_sizeB = font.MeasureString("Bullets: " + AirSimulation.statechange.NumProiettili);
            rendering_data.sfondo1 = Game.Content.Load<Texture2D>("sfondogame");
            rendering_data.sfondo2 = Game.Content.Load<Texture2D>("sfondo2");
            rendering_data.can3d = Game.Content.Load<Model>("slingshot");
            rendering_data.roccia = Game.Content.Load<Model>("Asteroid");
            rendering_data.aereo = Game.Content.Load<Model>("me309");
            pause = Game.Content.Load<Texture2D>("pause");
            pauses = Game.Content.Load<Texture2D>("pauses");
            medal = Game.Content.Load<Texture2D>("medaglia");
            rendering_data.soundExplosion = Game.Content.Load<SoundEffect>("explode");
            rendering_data.soundExplosionI = rendering_data.soundExplosion.CreateInstance();
            rendering_data.meshAereo = rendering_data.aereo.Meshes[0];
            rendering_data.meshRoccia = rendering_data.aereo.Meshes[0];
            mirino = Game.Content.Load<Texture2D>("mirino");
            //PER IL 2D
            //Confini considerando la grandezza degli aerei
            //il * 2 su rendering_data.MaxX è dovuto al fatot che ho girato l'aereo di 1.5f per disegnarlo e girandomelo si è traslato indietro di mezzo raggio
            rendering_data.MaxX = GraphicsDevice.Viewport.Width + (rendering_data.meshAereo.BoundingSphere.Radius * 2);
            rendering_data.MaxY = GraphicsDevice.Viewport.Height + (rendering_data.meshAereo.BoundingSphere.Radius * 2);
            rendering_data.MinX = 0.0f - (rendering_data.meshAereo.BoundingSphere.Radius );
            rendering_data.MinY = 0.0f - (rendering_data.meshAereo.BoundingSphere.Radius * 2);
            rendering_data.numeroMaxEsp = 30;
            rendering_data.Animations = new Animationsprite[rendering_data.numeroMaxEsp];
            rendering_data.AnimX = new float[rendering_data.numeroMaxEsp];
            rendering_data.AnimY = new float[rendering_data.numeroMaxEsp];
            rendering_data.animation_count = 0;
            rendering_data.changelevel = false;
            rendering_data.finepausa = false;
            base.LoadContent();
        }

        public override void Draw(GameTime gameTime)
        {
            //GraphicsDevice.Clear(Color.CornflowerBlue);
            
            //3D


            basic_effect.CurrentTechnique.Passes[0].Apply();
            Matrix[] transforms;
            //SASSO FERMO FIONDA
            if ((int)AirSimulation.statechange.NumProiettili > 0)
            {
                transforms = new Matrix[rendering_data.roccia.Bones.Count];
                rendering_data.roccia.CopyAbsoluteBoneTransformsTo(transforms);
                for (int i = 0; i < rendering_data.roccia.Meshes.Count; i++)
                {
                    ModelMesh mesh = rendering_data.roccia.Meshes[i];
                    foreach (BasicEffect fx in mesh.Effects)
                    {
                        fx.EnableDefaultLighting();
                        fx.SpecularColor = Color.Gray.ToVector3();
                        fx.View = basic_effect.View;
                        fx.Projection = basic_effect.Projection;
                        if ((G.GraphicsDevice.Viewport.Width == 800) && (G.GraphicsDevice.Viewport.Height == 480))
                            fx.World = basic_effect.World * Matrix.CreateFromYawPitchRoll(0.0f, 0.0f, (float)gameTime.TotalGameTime.TotalSeconds) * Matrix.CreateTranslation(new Vector3((rendering_data.MaxX + rendering_data.MinX) / 5.5f + (153.0f * (float)System.Math.Atan2(500.0f, (InputManagerG.InputManagerGame.GetState().mouseX - ((rendering_data.MaxX + rendering_data.MinX) / 2.0f)))) + GraphicsDevice.Viewport.Width / 2, -((rendering_data.MaxY + rendering_data.MinY) - ((rendering_data.MaxY + rendering_data.MinY) / 5.2f)) + GraphicsDevice.Viewport.Height / 2, 0.0f)) * mesh.ParentBone.Transform;
                        else
                            if ((G.GraphicsDevice.Viewport.Width == 800) && (G.GraphicsDevice.Viewport.Height == 600))
                                fx.World = basic_effect.World * Matrix.CreateFromYawPitchRoll(0.0f, 0.0f, (float)gameTime.TotalGameTime.TotalSeconds) * Matrix.CreateTranslation(new Vector3((rendering_data.MaxX + rendering_data.MinX) / 5.5f + (153.0f * (float)System.Math.Atan2(500.0f, (InputManagerG.InputManagerGame.GetState().mouseX - ((rendering_data.MaxX + rendering_data.MinX) / 2.0f)))) + GraphicsDevice.Viewport.Width / 2, -((rendering_data.MaxY + rendering_data.MinY) - ((rendering_data.MaxY + rendering_data.MinY) / 5.2f)) + GraphicsDevice.Viewport.Height / 2, 0.0f)) * mesh.ParentBone.Transform;
                            else
                                if ((G.GraphicsDevice.Viewport.Width == 1024) && (G.GraphicsDevice.Viewport.Height == 768))
                                    fx.World = basic_effect.World * Matrix.CreateFromYawPitchRoll(0.0f, 0.0f, (float)gameTime.TotalGameTime.TotalSeconds) * Matrix.CreateTranslation(new Vector3((rendering_data.MaxX + rendering_data.MinX) / 5.2f + (185.0f * (float)System.Math.Atan2(500.0f, (InputManagerG.InputManagerGame.GetState().mouseX - ((rendering_data.MaxX + rendering_data.MinX) / 2.0f)))) + GraphicsDevice.Viewport.Width / 2, -((rendering_data.MaxY + rendering_data.MinY) - ((rendering_data.MaxY + rendering_data.MinY) / 5.4f)) + GraphicsDevice.Viewport.Height / 2, 0.0f)) * mesh.ParentBone.Transform;
                                else
                                    fx.World = basic_effect.World * Matrix.CreateFromYawPitchRoll(0.0f, 0.0f, (float)gameTime.TotalGameTime.TotalSeconds) * Matrix.CreateTranslation(new Vector3((rendering_data.MaxX + rendering_data.MinX) / 4.8f + (220.0f * (float)System.Math.Atan2(500.0f, (InputManagerG.InputManagerGame.GetState().mouseX - ((rendering_data.MaxX + rendering_data.MinX) / 2.0f)))) + GraphicsDevice.Viewport.Width / 2, -((rendering_data.MaxY + rendering_data.MinY) - ((rendering_data.MaxY + rendering_data.MinY) / 5.1f)) + GraphicsDevice.Viewport.Height / 2, 0.0f)) * mesh.ParentBone.Transform;
                    }

                    mesh.Draw();
                }
            }
            
            //SASSO
            transforms = new Matrix[rendering_data.roccia.Bones.Count];
            rendering_data.roccia.CopyAbsoluteBoneTransformsTo(transforms);
            foreach (var p in AirSimulation.statechange.Proiettili.Value)
                for (int i = 0; i < rendering_data.roccia.Meshes.Count; i++)
                {
                    ModelMesh mesh = rendering_data.roccia.Meshes[i];
                    foreach (BasicEffect fx in mesh.Effects)
                    {
                        fx.EnableDefaultLighting();
                        fx.SpecularColor = Color.Gray.ToVector3();

                        fx.View = basic_effect.View;
                        fx.Projection = basic_effect.Projection;
                        fx.World = basic_effect.World * Matrix.CreateFromYawPitchRoll(0.0f, 0.0f, (float)gameTime.TotalGameTime.TotalSeconds) * Matrix.CreateTranslation(new Vector3(new Vector2(p.PosP.Value.X + GraphicsDevice.Viewport.Width / 2, -(p.PosP.Value.Y) + GraphicsDevice.Viewport.Height / 2), 0.0f)) * mesh.ParentBone.Transform;
                       // DebugShapeRenderer.AddBoundingSphere(new BoundingSphere(new Vector3(p.BoundP.Value.Center.X + GraphicsDevice.Viewport.Width / 2, -(p.BoundP.Value.Center.Y) + GraphicsDevice.Viewport.Height / 2, p.BoundP.Value.Center.Z), p.BoundP.Value.Radius), Color.Red);
                    }

                    mesh.Draw();
                    //DebugShapeRenderer.Draw(gameTime, basic_effect.View, basic_effect.Projection);
                    //System.Console.WriteLine("1sasso"+mesh.Name);
                }

            //FIONDA
            transforms = new Matrix[rendering_data.can3d.Bones.Count];
            rendering_data.can3d.CopyAbsoluteBoneTransformsTo(transforms);
            for (int i = 0; i < rendering_data.can3d.Meshes.Count; i++)
            {
                ModelMesh mesh = rendering_data.can3d.Meshes[i];
                foreach (BasicEffect fx in mesh.Effects)
                {
                    fx.EnableDefaultLighting();
                    fx.SpecularColor = Color.LightGray.ToVector3();

                    Vector3 poscan = new Vector3(new Vector2(-1.0f, -12.0f), -3.0f);
                    fx.View = Matrix.CreateLookAt(
                            Vector3.Backward * 20.0f + Vector3.Up ,
                            Vector3.Up,
                            Vector3.Up);
                    fx.Projection = Matrix.CreatePerspectiveFieldOfView(
                            1.5f,
                            GraphicsDevice.Viewport.AspectRatio,
                            0.01f, 100.0f);

                    //appunti miei
                    //math.atan non gestisce lo zero come angolo quindi quando passo il mouse da coordinate assex positivo ad asse x negativo
                    //mi cambia completamente l'angolo, per gestire in modo omogeneo questo cambiamento basta usare atam2
                    //il 500.0f indica quanto deve variare l'angolo vicino all'asse x quindi vicino allo 0, se è basso es 50, varia molto l'angolo vicino allo 0, se invece è alto l'angolo varia di poco vicino all'asse 0
                    // il - GraphicsDevice.Viewport.Width / 2 serve per spostare l'asse 0 al posto della vera asse zero che sarebbe sull bordo sinistro, con la sottrazione sopra la sposto al centro dello schermo
                    float angle = (float)(System.Math.Atan2((500.0f), InputManagerG.InputManagerGame.GetState().mouseX - GraphicsDevice.Viewport.Width / 2));
                    fx.World = Matrix.CreateWorld(poscan, Vector3.Forward, Vector3.Up) *Matrix.CreateRotationY(angle) * mesh.ParentBone.Transform;          
                }
                //System.Console.WriteLine("3fionda"+mesh.Name);
                mesh.Draw();

            }
            //AEREO
            transforms = new Matrix[rendering_data.aereo.Bones.Count];
            rendering_data.aereo.CopyAbsoluteBoneTransformsTo(transforms);
            foreach (var a in AirSimulation.statechange.Aerei.Value)
                for (int i = 0; i < rendering_data.aereo.Meshes.Count; i++)
                {
                    ModelMesh mesh = rendering_data.aereo.Meshes[i];
                    foreach (BasicEffect fx in mesh.Effects)
                    {
                        fx.EnableDefaultLighting();
                        //fx.DiffuseColor = Color.DarkGreen.ToVector3();
                        fx.SpecularColor = Color.Gray.ToVector3();

                        fx.View = basic_effect.View;
                        fx.Projection = basic_effect.Projection;
                        //appunti
                        //il -22 sulla y serve per centrare l'aereo nella sua posizione reale, xkè il modello dell'aereo non è centrato negli assi
                        //il - rendering_data.meshAereo.BoundingSphere.Radius o + sulla x, serve sempre per centrarlo nella sua reale posizione xkè altrimenti sarebbe spostato
                        //il +3ed il - 18 servono per centrarli correttamente sulla bounding box creata, xkè il modello dell'aereo non era centrato negli assi
                        if(a.VelA.Value>0)
                            fx.World = basic_effect.World * Matrix.CreateRotationY(-1.5f) * Matrix.CreateTranslation(new Vector3(new Vector2(a.PosA.Value.X + 3.0f - rendering_data.meshAereo.BoundingSphere.Radius + GraphicsDevice.Viewport.Width / 2, -(a.PosA.Value.Y) - 22.0f + GraphicsDevice.Viewport.Height / 2), 0.0f)) * mesh.ParentBone.Transform; //aggiungere basic_effect.World non cambia
                        else
                            fx.World = basic_effect.World * Matrix.CreateRotationY(+1.5f) * Matrix.CreateTranslation(new Vector3(new Vector2(a.PosA.Value.X - 18.0f +rendering_data.meshAereo.BoundingSphere.Radius + GraphicsDevice.Viewport.Width / 2, -(a.PosA.Value.Y) -22.0f + GraphicsDevice.Viewport.Height / 2), 0.0f)) * mesh.ParentBone.Transform; //aggiungere basic_effect.World non cambia
                        //DebugShapeRenderer.AddBoundingBox(new BoundingBox(new Vector3(a.BoundA.Value.Min.X + GraphicsDevice.Viewport.Width / 2, -(a.BoundA.Value.Min.Y) + GraphicsDevice.Viewport.Height / 2, a.BoundA.Value.Min.Z), new Vector3(a.BoundA.Value.Max.X + GraphicsDevice.Viewport.Width / 2, -(a.BoundA.Value.Max.Y) + GraphicsDevice.Viewport.Height / 2, a.BoundA.Value.Max.Z)), Color.Green);
                    }

                    mesh.Draw();
                    //DebugShapeRenderer.Draw(gameTime, basic_effect.View, basic_effect.Projection);
                    //System.Console.WriteLine("aereo2"+mesh.Name);
                }

            //2D
            //DepthStencilState.Default permette di visualizzare il 3d ma lo sfondo deve essere disegnato ad 1.0f
            spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend, SamplerState.AnisotropicClamp, DepthStencilState.Default, null);
            
            spriteBatch.DrawString(
                    font,
                    "Points: " + (int)AirSimulation.statechange.Points,
                    new Vector2(G.GraphicsDevice.Viewport.Width - string_sizeP.X, G.GraphicsDevice.Viewport.Height - G.GraphicsDevice.Viewport.Height / 6) - string_sizeP * 0.5f, // - string_size * 0.5f posso fare anche a meno di metterli, servono per mouvere la scritta in un posto più corretto cioè in modo che la posizione che noi diciamo sia proprio la posizione al centro della scritta che verrà scritta
                    Color.Black);
            spriteBatch.DrawString(
                    font,
                    "Bullets: " + (int)AirSimulation.statechange.NumProiettili,
                    new Vector2(G.GraphicsDevice.Viewport.Width / 7, G.GraphicsDevice.Viewport.Height - G.GraphicsDevice.Viewport.Height / 6) - string_sizeB * 0.5f, // - string_size * 0.5f posso fare anche a meno di metterli, servono per mouvere la scritta in un posto più corretto cioè in modo che la posizione che noi diciamo sia proprio la posizione al centro della scritta che verrà scritta
                    Color.Black);


            //CAMBIO LIVELLO
            if (rendering_data.changelevel == true)
            {
                if ((int)AirSimulation.statechange.Level == 4)
                {
                    spriteBatch.DrawString(
                    font,
                    "GAME COMPLETE!! ",
                    new Vector2(G.GraphicsDevice.Viewport.Width / 2 - string_sizeP.X, G.GraphicsDevice.Viewport.Height - G.GraphicsDevice.Viewport.Height / 2 - G.GraphicsDevice.Viewport.Height / 4),
                    Color.Black);
                    #if WINDOWS
                    spriteBatch.DrawString(
                    font,
                    "Press left mouse button to continue",
                    new Vector2(G.GraphicsDevice.Viewport.Width / 2 - (int)(G.GraphicsDevice.Viewport.Width / 2.7f), G.GraphicsDevice.Viewport.Height - G.GraphicsDevice.Viewport.Height / 3),
                    Color.Black);
                    #elif WINDOWS_PHONE
                    spriteBatch.DrawString(
            font,
            "Touch to continue",
            new Vector2(G.GraphicsDevice.Viewport.Width / 2 - string_sizeP.X, G.GraphicsDevice.Viewport.Height - G.GraphicsDevice.Viewport.Height / 2 - G.GraphicsDevice.Viewport.Height / 5),
            Color.Black);        
#endif
                    spriteBatch.Draw(medal, new Rectangle((int)(GraphicsDevice.Viewport.Width / 3.5f), (int)(GraphicsDevice.Viewport.Height / 2.6f), (int)(GraphicsDevice.Viewport.Width / 2.5f), GraphicsDevice.Viewport.Height / 6), new Rectangle(15, 15, 639, 200), Color.White, 0.0f, Vector2.Zero, SpriteEffects.None, 0.5f);
                }
                else
                {
                    if ((int)AirSimulation.statechange.Level != 0)
                    {
                        spriteBatch.DrawString(
                        font,
                        "Level Complete ",
                        new Vector2(G.GraphicsDevice.Viewport.Width / 2 - string_sizeP.X, G.GraphicsDevice.Viewport.Height - G.GraphicsDevice.Viewport.Height / 2 - G.GraphicsDevice.Viewport.Height / 5),
                        Color.Black);
                    }
                    spriteBatch.DrawString(
                    font,
                    "Level " + (int)AirSimulation.statechange.Level + " Start ",
                    new Vector2(G.GraphicsDevice.Viewport.Width / 2 - string_sizeP.X, G.GraphicsDevice.Viewport.Height - G.GraphicsDevice.Viewport.Height / 2 - G.GraphicsDevice.Viewport.Height / 8),
                    Color.Black);
                }
            }
            else
                if (rendering_data.pauSelez == true)
                    spriteBatch.Draw(pauses, new Rectangle((int)(GraphicsDevice.Viewport.Width / 42.5f), GraphicsDevice.Viewport.Height / 2 + (int)(GraphicsDevice.Viewport.Height / 2.8f), (int)(GraphicsDevice.Viewport.Width / 3.5f), GraphicsDevice.Viewport.Height / 8), new Rectangle(80, 60, 440, 150), Color.White, 0.0f, Vector2.Zero, SpriteEffects.None, 0.5f);
                else
                    spriteBatch.Draw(pause, new Rectangle((int)(GraphicsDevice.Viewport.Width / 42.5f), GraphicsDevice.Viewport.Height / 2 + (int)(GraphicsDevice.Viewport.Height / 2.8f), (int)(GraphicsDevice.Viewport.Width / 3.5f), GraphicsDevice.Viewport.Height / 8), new Rectangle(80, 60, 440, 150), Color.White, 0.0f, Vector2.Zero, SpriteEffects.None, 0.5f);

            //FINE PAUSA
#if WINDOWS_PHONE
            if (rendering_data.finepausa == true)
            {
                spriteBatch.DrawString(
                            font,
                            "Touch to continue",
                            new Vector2(G.GraphicsDevice.Viewport.Width / 2 - string_sizeP.X, G.GraphicsDevice.Viewport.Height - G.GraphicsDevice.Viewport.Height / 2 - G.GraphicsDevice.Viewport.Height / 5),
                            Color.Black);
            }
#endif
            //ANIMAZIONE ESPLOSIONE
            //disegno tutte le esplosioni
            for (int i = 0; i < rendering_data.animation_count; i++)
            {
                if (rendering_data.Animations[i].getEndAnimation() == false)
                    spriteBatch.Draw(rendering_data.Animations[i].spritesheet, new Vector2(rendering_data.AnimX[i], rendering_data.AnimY[i]), rendering_data.Animations[i].sourceRect, Color.White, 0.0f, rendering_data.Animations[i].spriteCenter, 1.0f, SpriteEffects.None, 0.5f);
            }

            

            #if WINDOWS
            spriteBatch.Draw(mirino, new Vector2(InputManagerG.InputManagerGame.inputState.mouseX - mirino.Width * 0.5f, InputManagerG.InputManagerGame.inputState.mouseY - mirino.Height * 0.5f), new Rectangle(0, 0, 300, 300), Color.White, 0.0f, Vector2.Zero, 1.00f, SpriteEffects.None, 0.0f);
#endif
            if ((int)AirSimulation.statechange.Level < 2)
            {
                #if WINDOWS
                //sfondo uno
                spriteBatch.Draw(rendering_data.sfondo2, new Rectangle(0, 0, GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height), new Rectangle(0, 0, 1601, 1081), Color.White, 0.0f, Vector2.Zero, SpriteEffects.None, 1.0f);
           #elif WINDOWS_PHONE
                spriteBatch.Draw(rendering_data.sfondo2, new Rectangle(0, 0, GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height), new Rectangle(0, 0, 800, 480), Color.White, 0.0f, Vector2.Zero, SpriteEffects.None, 1.0f);
                        #endif
                }
            else
            {
                #if WINDOWS
                //sfondo due
                spriteBatch.Draw(rendering_data.sfondo1, new Rectangle(0, 0, GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height), new Rectangle(0, 0, 3072, 2304), Color.White, 0.0f, Vector2.Zero, SpriteEffects.None, 1.0f);
            #elif WINDOWS_PHONE
                spriteBatch.Draw(rendering_data.sfondo1, new Rectangle(0, 0, GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height), new Rectangle(0, 0, 800, 480), Color.White, 0.0f, Vector2.Zero, SpriteEffects.None, 1.0f);
#endif
            }
            spriteBatch.End();

            ////3D old

            //basic_effect.CurrentTechnique.Passes[0].Apply();
            //Matrix[] transforms = new Matrix[rendering_data.can3d.Bones.Count];
            //rendering_data.can3d.CopyAbsoluteBoneTransformsTo(transforms);
            //// ModelBone torretta = rendering_data.can3d.Bones["Object03"];
            //// torretta.Transform = Matrix.CreateRotationZ((float)gameTime.TotalGameTime.TotalSeconds)* Matrix.CreateScale(2.3f);
            //for (int i = 0; i < rendering_data.can3d.Meshes.Count; i++)
            //{
            //    ModelMesh mesh = rendering_data.can3d.Meshes[i];
            //    foreach (BasicEffect fx in mesh.Effects)
            //    {
            //        fx.EnableDefaultLighting();
            //        //fx.DiffuseColor = Color.DarkGreen.ToVector3();
            //        fx.SpecularColor = Color.Gray.ToVector3();

            //        fx.View = basic_effect.View ;
            //        fx.Projection = basic_effect.Projection;

            //        //if (i == 0)
            //        //    fx.World = basic_effect.World * Matrix.CreateRotationZ((float)gameTime.TotalGameTime.TotalSeconds) *
            //        //         transforms[mesh.ParentBone.Index];
            //        //else
            //        //    fx.World = basic_effect.World * transforms[mesh.ParentBone.Index] * Matrix.CreateRotationY(-2.1f);
            //        fx.World = basic_effect.World * mesh.ParentBone.Transform * Matrix.CreateRotationY(-1.1f);
            //    }

            //    mesh.Draw();
            //}

            //altrimenti, senza effetti però
            //rendering_data.can3d.Draw(
            // basic_effect.World * Matrix.CreateRotationY(-2.1f),
            // basic_effect.View,
            // basic_effect.Projection);

            base.Draw(gameTime);
        }
        protected override void Dispose(bool disposing)
        {
            Game.Services.RemoveService(typeof(renderingdata));
            base.Dispose(disposing);
        }
    }
}
